/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ui;

import font.MyFont;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;

/**
 *
 * @author kien.tran
 */
public class CustomImageViewer {

    public static final int STEP = 20;
    public static final int UP = -1;
    public static final int DOWN = 1;
    public static final int LEFT = -1;
    public static final int RIGHT = 1;
    private Image image;
    private Image arrow;
    private Image paintedImage;
    private String message;
    private MyFont font;
    private Style style;
    private int imageHeight;
    private int imageWidth;
    private int screenHeight;
    private int screenWidth;
    private int x;
    private int y;

    public CustomImageViewer(Image image, int width, int height, String message, Style style, MyFont font) {
        this.font = font;
        this.style = style;
        this.image = image;
        this.screenWidth = width;
        this.screenHeight = height;
        this.message = message;

        if (image != null) {
            imageWidth = image.getWidth();
            imageHeight = image.getHeight();
        }

        x = 0;
        y = 0;
    }

    private void processImage() {
        if (arrow == null) {
            try {
                arrow = Image.createImage("/image/arrow.png");
            } catch (java.io.IOException err) {
                arrow = null;
            }
        }

        paintedImage = null;
        if (image != null) {
            int w = screenWidth;
            int h = screenHeight;

            if (imageWidth < screenWidth) {
                w = imageWidth;
            }

            if (imageHeight < screenHeight) {
                h = imageHeight;
            }

            Image temp = Image.createImage(image);
            paintedImage = Image.createImage(temp, x, y, w, h, Sprite.TRANS_NONE);

            if (temp != null) {
                temp = null;
            }

            System.gc();
        }
    }

    public void paint(Graphics g) {
        g.setColor(style.getBgColor());
        g.fillRect(0, 0, screenWidth, screenHeight);
        processImage();

        if (paintedImage != null) {
            g.drawImage(paintedImage, 0, 0, Graphics.TOP | Graphics.LEFT);

            if (arrow != null) {
                Image top = null;
                int t = arrow.getHeight();
                int t1 = x + screenWidth;
                int t2 = y + screenHeight;

                if (x > 0) {
                    top = Image.createImage(arrow, 0, 0, arrow.getWidth(), arrow.getHeight(), Sprite.TRANS_MIRROR_ROT90);
                    g.drawImage(top, t, screenHeight / 2, Graphics.VCENTER | Graphics.HCENTER);
                }

                if (y > 0) {
                    top = Image.createImage(arrow, 0, 0, arrow.getWidth(), arrow.getHeight(), Sprite.TRANS_MIRROR_ROT180);
                    g.drawImage(top, screenWidth / 2, t, Graphics.VCENTER | Graphics.HCENTER);
                }

                if (t1 < imageWidth) {
                    top = Image.createImage(arrow, 0, 0, arrow.getWidth(), arrow.getHeight(), Sprite.TRANS_MIRROR_ROT270);
                    g.drawImage(top, screenWidth - t, screenHeight / 2, Graphics.VCENTER | Graphics.HCENTER);
                }

                if (t2 < imageHeight) {
                    top = Image.createImage(arrow, 0, 0, arrow.getWidth(), arrow.getHeight(), Sprite.TRANS_NONE);
                    g.drawImage(top, screenWidth / 2, screenHeight - t, Graphics.VCENTER | Graphics.HCENTER);
                }
            }

        } else {
            font.getFontNormal().drawString(g, message, screenWidth / 2, style.getPadding().getTop(), Graphics.TOP | Graphics.HCENTER);
        }
    }

    public void actionLeftRight(int step) {
        if (step < 0) {
            if (x > 0) {
                int t = x - STEP;
                if (t >= 0) {
                    x -= STEP;
                } else {
                    x = 0;
                }
            }
        } else if (step > 0) {
            int t = x + screenWidth;
            if (t < imageWidth) {
                int t2 = t + STEP;
                if (t2 <= imageWidth) {
                    x += STEP;
                } else {
                    x = imageWidth - screenWidth;
                }
            }
        }
    }

    public void actionUpDown(int step) {
        if (step < 0) {
            if (y > 0) {
                int t = y - STEP;
                if (t >= 0) {
                    y -= STEP;
                } else {
                    y = 0;
                }
            }
        } else if (step > 0) {
            int t = y + screenHeight;
            if (t < imageHeight) {
                int t2 = t + STEP;
                if (t2 <= imageHeight) {
                    y += STEP;
                } else {
                    y = imageHeight - screenHeight;
                }
            }
        }
    }
}
